package am.fp.toolbars
{ 
  import fl.controls.Button;                    

  import flash.events.Event;
  
  import am.fp.Toolbar;
  import am.fp.FPDispatcher;
  import am.fp.events.ModeEvent;
  
  /**
   * Toolbar containing buttons to switch active mode (grab or text selection).
   */
  public class Mode extends Toolbar
  { 
    private var m_btnGrab: Button;
    private var m_btnText: Button;
    
    /**
     * Constructor. Add buttons and connect listeners.
     */
    public function Mode()
    {
      m_btnGrab = addButton("GrabIcon");
      m_btnText = addButton("TextIcon");
      m_btnGrab.toggle = true;
      m_btnText.toggle = true;
      grabMode = true;
      
      m_btnGrab.addEventListener(Event.CHANGE, onGrabMode, false, 0, true);
      m_btnText.addEventListener(Event.CHANGE, onTextMode, false, 0, true);
      FPDispatcher.instance.addEventListener(ModeEvent.CHANGE, onModeChange, false, 0, true);
    }
    
    private function set grabMode(activated: Boolean)
    {
      m_btnGrab.selected = activated;
      m_btnText.selected = !activated;
    }
    
    private function onGrabMode(e: Event): void
    {                                         
      FPDispatcher.instance.dispatchEvent(new ModeEvent(ModeEvent.CHANGE, ModeEvent.MODE_GRAB));
    }

    private function onTextMode(e: Event): void
    {      
      FPDispatcher.instance.dispatchEvent(new ModeEvent(ModeEvent.CHANGE, ModeEvent.MODE_TEXT));
    }
    
    private function onModeChange(e: ModeEvent): void
    {
      grabMode = (e.mode == ModeEvent.MODE_GRAB);
    }
  }
}

